package com.lzh.persevere.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzh.persevere.mapper.CategoryInfoMapper;
import com.lzh.persevere.model.entity.CategoryInfo;
import com.lzh.persevere.model.qo.PageModelQO;
import com.lzh.persevere.service.CategoryInfoService;
import com.lzh.persevere.template.PageAbstractTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 分类表服务实现类
 *
 * @author luzhiheng
 * @since 2025-07-17
 */
@Service
public class CategoryInfoServiceImpl extends ServiceImpl<CategoryInfoMapper, CategoryInfo> implements CategoryInfoService {

    @Resource
    private RedisCacheService redisCacheService;

    @Override
    public Map<String, Object> findCategoryPage(PageModelQO qo) {
        Map<String, Object> otherQueryMap = Collections.emptyMap();
        PageAbstractTemplate template = new PageAbstractTemplate() {
            @Override
            public Integer countTotal() {
                return baseMapper.countList(this.getQueryMap(qo.getPageModel(), otherQueryMap));
            }

            @Override
            public List<Map<String, Object>> queryPageList() {
                List<Map<String, Object>> pagedList = baseMapper.pageList(this.queryMap);
                if (CollectionUtil.isNotEmpty(pagedList)) {
                    Map<String, String> allUserIdNickNameMap = redisCacheService.getAllUserIdNickNameMap();
                    for (Map<String, Object> map : pagedList) {
                        map.put("createNickName", allUserIdNickNameMap.get(String.valueOf(map.get("createUserId"))));
                        map.put("updateNickName", allUserIdNickNameMap.get(String.valueOf(map.get("updateUserId"))));
                    }
                }
                return pagedList;
            }
        };
        return template.queryPage();
    }
}
